/*
 * To change this template, choose
 pruebita| Templates
 * and open the template in the editor.
 */

package CONTROLES;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrador
 */
public  class Conexion {
    public static Connection conection;
    Properties props = new Properties();
    FileInputStream s;

    private void iniciarConexion() {
         File f = new File("\\configuracionbd.txt").getAbsoluteFile();
        try {
           
            s = new FileInputStream(f);
            props.load(s);
            Class.forName("org.postgresql.Driver");
            //String url = "jdbc:postgresql://localhost:5432/nfn1";
            String url = props.getProperty("url");
            //String usuario = "postgres";
            String usuario = props.getProperty("usuario");
            //String clave = "postgres";
            String clave = props.getProperty("clave");
            conection = DriverManager.getConnection(url, usuario, clave);
            System.out.println("conexion creada");
        } catch (IOException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        } 
         catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        catch (SQLException ex) {
                Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

    public Conexion() {
        if(conection==null){
            iniciarConexion();
        }else{
            try {
                if (conection.isClosed()) {
                    iniciarConexion();
                }
            } catch (SQLException ex) {
                Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /*
     * Metodo para obetener una Conexion a la base de datos
     */
    public Connection getConexion()  {
        try {
            if (conection.isClosed()) {
                iniciarConexion();
                return getConexion();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return  conection;
    }




}
